package com.jwlifer.manager.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jwlifer.dao.domain.JwliferAdmin;
import com.jwlifer.service.jwliferAdminService;
import com.jwlifer.util.model.ReturnObject;

@Controller
@RequestMapping("/jwlifer")
public class AdminController {

	@Autowired
	jwliferAdminService adminService;
	
	@RequestMapping("/login")
	@ResponseBody
	public ReturnObject addJwliferAdmin(
			@RequestParam(value = "userName", required = false) String userName,
			@RequestParam(value = "email", required = false) String email){
		System.out.println("jerry");
		ReturnObject rt = new ReturnObject();
		rt.setCode("0");
		if(StringUtils.isBlank(userName)){
			rt.setObj("username can not be null!");
			return rt;
		}
		if(StringUtils.isBlank(email)){
			rt.setObj("email can not be null!");
			return rt;
		}
		try {
			JwliferAdmin admin = new JwliferAdmin();
			admin.setUserName(userName);
			admin.setEmail(email);
			int cnt = adminService.addJwliferAdmin(admin);
			if(1 == cnt){
				rt.setCode("1");
				rt.setObj("Add admin success!");
			}
		} catch (Exception e) {
			// TODO: handle exception
			rt.setObj("Exception occured: "+e.getMessage());
		}
		return rt;
	}
}
